package com.example.a10928.weifriend.ui.personal_center

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.ui.BaseFragment
import kotlinx.android.synthetic.main.fragment_personal_center.*
import android.widget.Toast
import com.example.a10928.weifriend.ui.MainActivity
import android.graphics.Color.DKGRAY
import android.support.v4.widget.SwipeRefreshLayout
import android.text.TextUtils
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.http.api.base.UserMsgBean
import com.example.a10928.weifriend.ui.login.LoginActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import com.mylhyl.circledialog.CircleDialog
import com.squareup.picasso.Picasso
import io.reactivex.functions.Consumer


class PersonalCenterFragment : BaseFragment(),SwipeRefreshLayout.OnRefreshListener {
    override fun onRefresh() {
        getUserInfo()
    }

    var userMsgBean:UserMsgBean?=null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_personal_center, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        swipe.setOnRefreshListener(this)
        getUserInfo()


        item_bind_pay.setOnClickListener {
/*            if( userMsgBean!=null&&userMsgBean?.user_message?.is_bind_alipay.equals("1")){
                L.t("已绑定支付宝")
                return@setOnClickListener
            }*/
            var intent= Intent(activity,BindPayActivity::class.java)

            startActivity(intent)
        }

        item_my_wallet.setOnClickListener {
            var intent= Intent(activity,MyWalletActivity::class.java)
            startActivity(intent)
        }

        item_my_information.setOnClickListener {
            var intent= Intent(activity,PersonalInfoActivity::class.java)
            startActivity(intent)
        }

        item_my_team.setOnClickListener {
            var intent= Intent(activity,MyTeamActivity::class.java)
            startActivity(intent)
        }

        item_my_adert.setOnClickListener {
            var intent= Intent(activity,MyAdertActivity::class.java)
            startActivity(intent)
        }

        item_bind_bank.setOnClickListener {
            var intent= Intent(activity,MyBankActivity::class.java)
            startActivity(intent)
        }


        item_edit_pw.setOnClickListener {
            var intent= Intent(activity,EditPWActivity::class.java)
            startActivity(intent)
        }

        item_exit_login.setOnClickListener {

            CircleDialog.Builder()
                    .setCanceledOnTouchOutside(false)
                    .setCancelable(false)
                    .setTitle("确认退出")
                    .setText("确认退出登录")
                    .setNegative("取消", null)
                    .setPositive("确定") { v ->
                        (activity as MainActivity).showProgress()

                        App.app().bpService.logout(App.app().user.userId)
                                .compose(RxUtil.io2main())
                                .subscribe(

                                        Consumer {
                                            (activity as MainActivity).dismmProgress()

                                            App.app().user.clearUser()
                                            var intent=Intent(activity,LoginActivity::class.java)
                                            startActivity(intent)
                                            activity?.finish()
                                        },
                                        Consumer {

                                            RxUtil.errorException(it)
                                        }
                                )

                    }
                    .configNegative { params ->  params.textColor= Color.RED }
                    .show(activity?.getSupportFragmentManager())


        }

        if(!TextUtils.isEmpty(App.app().user.headImg) ){
            Picasso.with(activity).load(App.app().user.headImg).into(iv_header)
        }

        tv_id.setText(App.app().user.account)





    }

    fun  getUserInfo(){

        (activity as MainActivity).showProgress()
        App.app().bpService.getUserHomeMsg(App.app().user.userId)
                .compose(RxUtil.io2main())
                .subscribe(
                        Consumer {
                            (activity as MainActivity).dismmProgress()
                            userMsgBean=it.data
                            App.app().user.saveHeadImg(it.data.user_message.user_headimg)
                            App.app().user.saveAccount(it.data.user_message.user_name)
                            tv_id.setText(App.app().user.userId+"")
                            Picasso.with(activity).load(App.app().user.headImg).into(iv_header)
                            tv_total.setText(it.data.user_message.into_amount.toString())
                            tv_today.setText(it.data.user_message.user_today_in.toString())
                            tv_withdraw.setText(it.data.user_message.withdraw_amount.toString())

                            if(it.data.user_message.user_auth_level==1){
                                tv_vip_level.setText("普通会员")
                            }else if(it.data.user_message.user_auth_level==2) {
                                tv_vip_level.setText("VIP会员")
                            }else if(it.data.user_message.user_auth_level==3) {
                                tv_vip_level.setText("销售经理")
                            } else {
                                tv_vip_level.setText("营销总监")
                            }

                            swipe.isRefreshing=false
                        },
                        Consumer {
                            RxUtil.errorException(it)
                            swipe.isRefreshing=false
                        }
                )
    }

}
